package com.spring.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.spring.service.UserService;
import com.spring.service.UserServiceImpl;

//动态代理
public class UserServiceProxyFactory implements InvocationHandler {
	private UserService us;
	
	public UserServiceProxyFactory(UserService us) {
		super();
		this.us = us;
	}
	public UserService getUserServiceProxy() {
		//生成动态代理
		UserService us = (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), this);
		return us;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("开始事务");
		Object obj = method.invoke(us, args);
		System.out.println("提交事务");
		return obj;
	}

	
}
